# Makefile for H.264 RTSP Live Streaming Application
# Based on Tassadar-T32 SDK documentation
# Cross-compiler toolchain version: mips-gcc540-glibc222-r3.3.7.mxu2.cve

# SDK path configuration (modify according to actual installation path)
# Using hard assignment to prevent override by environment or command line
SDK_PATH := $(HOME)/Desktop/My_Demo/Tassadar-T32-1.0.6-20250613/software
TOOLCHAIN_PATH := $(SDK_PATH)/pc/toolchain/mips-gcc540-glibc222-r3.3.7.mxu2.cve

# Cross-compilation tools
# Use the uclibc-prefixed toolchain wrapper to match SDK sample build settings
CROSS_COMPILE := $(TOOLCHAIN_PATH)/bin/mips-linux-uclibc-gnu-
CXX := $(CROSS_COMPILE)g++
CC := $(CROSS_COMPILE)gcc
LD := $(CROSS_COMPILE)ld
AR := $(CROSS_COMPILE)ar
RANLIB := $(CROSS_COMPILE)ranlib

# Include paths for headers
INCLUDE = -I. -I./encode -I./buffer -I./stream -I./service -I./examples \
          -I$(SDK_PATH)/board/Ingenic-SDK-T32/media/zh/include/ \
          -I$(TOOLCHAIN_PATH)/include/ \
          -I$(CURDIR)/live555_minimal/include \
          -I$(CURDIR)/live555_minimal/include/liveMedia \
          -I$(CURDIR)/live555_minimal/include/BasicUsageEnvironment \
          -I$(CURDIR)/live555_minimal/include/UsageEnvironment \
          -I$(CURDIR)/live555_minimal/include/groupsock

# Library paths and dependencies
LIBS = $(SDK_PATH)/board/Ingenic-SDK-T32/media/zh/lib/uclibc/libimp.a \
       $(SDK_PATH)/board/Ingenic-SDK-T32/media/zh/lib/uclibc/libalog.a \
       -L$(CURDIR)/live555_minimal/src/liveMedia \
       -L$(CURDIR)/live555_minimal/src/BasicUsageEnvironment \
       -L$(CURDIR)/live555_minimal/src/UsageEnvironment \
       -L$(CURDIR)/live555_minimal/src/groupsock \
       -lliveMedia -lBasicUsageEnvironment -lUsageEnvironment -lgroupsock \
       -lpthread -lrt -ldl -lm

# Linker flags
LDFLAG = -Wl,-gc-sections

# Compilation flags
CXXFLAGS = -std=c++11 -Wall -EL -O3 -march=mips32r2 -flax-vector-conversions -w -fpermissive -DNO_OPENSSL -DLOCALE_NOT_USED -fPIC $(INCLUDE)
CFLAGS = -Wall -EL -O3 -march=mips32r2 -flax-vector-conversions -w -fPIC $(INCLUDE)

# 编译目标
TARGET = live555-rtsp-h264

# Source files from different directories (四层架构)
# 编码层
ENCODE_SRCS = encode/sample-common.c encode/sample-Encoder-video.c
# 缓冲层
BUFFER_SRCS = buffer/ringfifo.c buffer/RingBuffer.cpp
# 推流层
STREAM_SRCS = stream/T32H264LiveSource.cpp stream/VideoStreamHandler.cpp stream/H264VideoServerMediaSubsession.cpp stream/RTSPServerManager.cpp
# 服务层
SERVICE_SRCS = service/Logger.cpp service/VideoLogger.cpp service/PerformanceLogger.cpp service/LogConfig.cpp
# 示例程序
EXAMPLES_SRCS = examples/main.c

# Combine all source files
SRCS = $(ENCODE_SRCS) $(BUFFER_SRCS) $(STREAM_SRCS) $(SERVICE_SRCS) $(EXAMPLES_SRCS)

# Generate object file names
OBJS = $(SRCS:.c=.o)
OBJS := $(OBJS:.cpp=.o)

# Output directory for object files
OBJ_DIR = obj

# Create object file paths
OBJ_FILES = $(addprefix $(OBJ_DIR)/, $(notdir $(OBJS)))

# Default target - build the executable
all: $(TARGET)

# Create object directory if it doesn't exist
$(OBJ_DIR):
	mkdir -p $(OBJ_DIR)

# 简化编译规则，确保所有源文件都能正确编译到obj目录
$(OBJ_DIR)/%.o: %.cpp | $(OBJ_DIR)
	$(CXX) -c $(CXXFLAGS) $< -o $@

$(OBJ_DIR)/%.o: %.c | $(OBJ_DIR)
	$(CC) -c $(CFLAGS) $< -o $@

# 为不同目录下的源文件添加编译规则（四层架构）
$(OBJ_DIR)/%.o: encode/%.c | $(OBJ_DIR)
	$(CC) -c $(CFLAGS) $< -o $@

$(OBJ_DIR)/%.o: buffer/%.c | $(OBJ_DIR)
	$(CC) -c $(CFLAGS) $< -o $@

$(OBJ_DIR)/%.o: buffer/%.cpp | $(OBJ_DIR)
	$(CXX) -c $(CXXFLAGS) $< -o $@

$(OBJ_DIR)/%.o: stream/%.cpp | $(OBJ_DIR)
	$(CXX) -c $(CXXFLAGS) $< -o $@

$(OBJ_DIR)/%.o: stream/%.c | $(OBJ_DIR)
	$(CC) -c $(CFLAGS) $< -o $@

$(OBJ_DIR)/%.o: service/%.cpp | $(OBJ_DIR)
	$(CXX) -c $(CXXFLAGS) $< -o $@

$(OBJ_DIR)/%.o: service/%.c | $(OBJ_DIR)
	$(CC) -c $(CFLAGS) $< -o $@

$(OBJ_DIR)/%.o: examples/%.c | $(OBJ_DIR)
	$(CC) -c $(CFLAGS) $< -o $@

# 通用规则（处理所有目录）
$(OBJ_DIR)/%.o: %.cpp | $(OBJ_DIR)
	$(CXX) -c $(CXXFLAGS) $< -o $@

$(OBJ_DIR)/%.o: %.c | $(OBJ_DIR)
	$(CC) -c $(CFLAGS) $< -o $@

# Linking rule to generate the executable
$(TARGET): $(OBJ_FILES)
	$(CXX) $(CXXFLAGS) $(LDFLAG) -o $@ $(OBJ_FILES) $(LIBS)

# Clean target - remove generated files
clean:
	rm -rfv $(TARGET) $(OBJ_DIR)/*.o

# Generate architecture optimization report
report:
	@echo "报告已生成: docs/Problem_Solution/日志系统统一管理报告.md"
# Dependency rules
.PHONY: all clean report